import java.util.Map;

import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSItem;
import com.speljohan.rsbot.script.wrappers.RSItemTile;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class RockCrabKiller extends Script {
	int[] CRAB_ROCK_MODE = { 1266, 1268 };
	int[] CRAB_NORMAL_MODE = { 1265, 1267 };
	public int ITPID1 = 0000;
	public int[] ITPIDs;
	public int ITPID2 = 0000;
	public int fightMode;
	public boolean doingspec = false;
	public long attacking;
	public boolean pickoi = false;
	int eatAT = 30;
	public int count = 0;
	int newcount = 0;
	public int[] FoodID = new int[] { 379, 385, 7946, 333, 2297, 2299 };
	RSTile[] PATH = { new RSTile(2707, 3721), new RSTile(2707, 3705),
			new RSTile(2706, 3691) };
	RSTile[] PATH2 = reversePath(PATH);

	public void doSpec() {
		if (getSpec() == 1000 && !specEnabled() && doingspec == true) {
			openTab(Constants.TAB_ATTACK);
			clickMouse(645 + random(0, 4), 425 + random(0, 4), true);
		}
		return;
	}

	void eat(final int id) {
		atInventoryItem(id, "Eat");
		wait(random(1500, 2500));
		return;
	}

	private void eat_check() {
		final RSItem food = getInventoryItemByID(FoodID);
		if (getInventoryCount(FoodID) >= 1) {
			eat(food.getID());
		} else {
			log("why is this here? idk");
		}
		return;
	}

	void EATING() {
		final int php = skills.getCurrentSkillLevel(3);
		if (php <= eatAT) {
			eat_check();
		} else {
			wait(random(20, 30));
		}
	}

	@Override
	public String getAuthor() {
		return "Killa";
	}

	@Override
	public String getName() {
		return "RockCrabKiller";
	}

	@Override
	public String getScriptCategory() {
		return "Combat";
	}

	@Override
	public String getScriptDescription() {
		String html = "";
		html += "<html>";
		html += "<head>";
		html += "<style type=\"text/css\"> hr {color: white} p {margin-left: 20px}</style>";
		html += "</head>";
		html += "<body>";
		html += "<center>";
		html += "<b><font size=\"6\" color=\"red\">" + getName() + " v"
				+ getVersion() + " c" + "</font></b><br>";
		html += "<font size=\"3\" color=\"red\">Please start @ the eastern Crabs.<br></font>";
		html += "</center>";
		html += "<center><table border=\"0\"><tr><td colspan=\"5\"><center><font size=6><b>== Options and Settings ==</b></font></center></td></tr><tr><td colspan=4><hr></td></tr><tbody><tr><td width=\"135\">";
		html += "<b>Use Special:</b></td><td width=\"140\"><center><input type=\"checkbox\" name=\"spec\" value=\"true\" checked=\"checked\">&nbsp<b>Yes</b></center></td><td width=\"135\"></b></center></td></tr>";
		html += "<tr><td><b>Eat At(30 defult):</b>&nbsp<input type=\"text\" name=\"eatt\" size=\"4\"</center></td></tr>";
		html += "<td><center><select name=\"attT\"><option>Strength<option selected>Attack<option>Defence<option ></center></td></tr>";
		html += "<tr><td><b>Pick Items:</b></td><td colspan=\"3\"><center><b>Item ID 1:</b>&nbsp<input type=\"text\" name=\"ItemID1\" size=\"4\">&nbsp&nbsp<b>Item ID 2:</b>&nbsp<input type=\"text\" name=\"ItemID2\" size=\"4\"></center></td></tr>";
		html += "<tr><td colspan=2><b><font size=\"3\">&nbsp&nbsp Beta! Enjoy!</font></b></td><td  colspan=\"2\" align=right>"
				+ getVersion() + " &nbsp&nbsp</td></tr></table>";
		html += "</center></body>";
		html += "</html>";
		return html;
	}

	public int getSpec() {
		return getSetting(300);
	}

	@Override
	public double getVersion() {
		return 1.2;
	}

	private boolean isThere() {
		return getMyPlayer().getLocation().getY() >= 3714;
	}

	@Override
	public int loop() {
		setFightMode(fightMode);
		setCameraAltitude(true);
		setCompass('N');
		if (getMyPlayer().isMoving()) {
			return random(0, 150);
		}
		if (isThere() && !getMyPlayer().isInCombat()
				&& getNearestNPCByID(CRAB_ROCK_MODE) != null
				&& getGroundItemByID(5, ITPIDs) == null) {
			Walking();
			wait(random(6000, 7000));
		}
		if (isThere() && !getMyPlayer().isInCombat()
				&& distanceTo(getNearestNPCByID(CRAB_NORMAL_MODE)) >= 3
				&& !getMyPlayer().isMoving()
				&& distanceTo(getNearestNPCByID(CRAB_ROCK_MODE)) <= 1
				&& getNearestNPCByID(CRAB_ROCK_MODE) != null) {
			walkPathMM(randomizePath(PATH, 2, 2), 20);
			clickMouse(630, 136, true);
			wait(random(4000, 4500));
			clickMouse(630, 136, true);
			wait(random(5000, 10000));
		}
		if (!isThere()) {
			walkPathMM(randomizePath(PATH2, 2, 2), 20);
		}
		if (getNearestNPCByID(CRAB_ROCK_MODE) != null
				&& distanceTo(getNearestNPCByID(CRAB_NORMAL_MODE)) <= 1
				&& !getNearestNPCByID(CRAB_ROCK_MODE).isInCombat()
				&& getMyPlayer().getInteracting() == null
				&& getNearestNPCByID(CRAB_ROCK_MODE).getInteracting() == null
				&& getGroundItemByID(5, ITPIDs) == null) {
			atNPC(getNearestFreeNPCByID(CRAB_NORMAL_MODE), "ack");
		}
		if (getMyPlayer().isInCombat()) {
			doSpec();
			EATING();
			wait(random(50, 100));
		}
		if (pickoi && !getMyPlayer().isInCombat()) {
			pickOI();
		}
		if (!isRunning()) {
			if (getEnergy() >= 50) {
				setRun(true);
			}

		}
		return random(50, 100);
	}

	@Override
	public boolean onStart(final Map<String, String> args) {
		if (args.get("attT").equals("Strength")) {
			attacking = skills.getXPToNextLevel(2);
			fightMode = 1;
		}
		if (!args.get("eatt").equals("")) {
			eatAT = Integer.parseInt(args.get("eatt"));
		}
		if (args.get("attT").equals("Attack")) {
			attacking = skills.getXPToNextLevel(0);
			fightMode = 0;
		}
		if (args.get("spec") != null) {
			if (args.get("spec").equals("true")) {
				doingspec = true;
			}
		}
		if (args.get("attT").equals("Defence")) {
			attacking = skills.getXPToNextLevel(1);
			fightMode = 3;
		}
		if (!args.get("ItemID1").equals("")) {
			ITPID1 = Integer.parseInt(args.get("ItemID1"));
			pickoi = true;
		}
		if (!args.get("ItemID2").equals("")) {
			ITPID2 = Integer.parseInt(args.get("ItemID2"));
			pickoi = true;
		}
		ITPIDs = new int[] { ITPID1, ITPID2, };
		return true;
	}

	public void pickOI() {
		getInventoryItemByID(ITPIDs);
		final int OldOitemCount = getInventoryCount(ITPIDs);
		final RSItemTile TItem = getGroundItemByID(5, ITPIDs);
		if (TItem != null) {
			atTile(TItem, "Take");
			while (getMyPlayer().isMoving()) {
				wait(15);
			}
			if (getInventoryCount(ITPIDs) > OldOitemCount) {
				count = count + 1;
				log("Picked up item.");
				newcount = count;
				return;
			} else {
				log("No item found.");
				return;
			}
		}
		return;
	}

	public boolean specEnabled() {
		return getSetting(301) == 1;
	}

	void Walking() {
		if (getNearestNPCByID(CRAB_ROCK_MODE).getLocation().getX() <= 2690) {
			wait(random(5, 10));
		} else {
			walkTileMM(getNearestNPCByID(CRAB_ROCK_MODE).getLocation());
		}
	}
}